﻿#include <iostream>

static void swap(int* pArr, int index1, int index2)
{
    if (index1 == index2) return;

    pArr[index1] = pArr[index1] ^ pArr[index2];
    pArr[index2] = pArr[index1] ^ pArr[index2];
    pArr[index1] = pArr[index1] ^ pArr[index2];
}

static void sort(int* numbers, const size_t size) {
    int min = numbers[0];
    int index = 0;
    int minIndex = 0;

    while (index < size) {
        bool foundMin = false;
        for (int i = index; i < size; i++) {
            if (numbers[i] < min) {
                foundMin = true;
                minIndex = i;
                min = numbers[i];
            }
        }

        if (foundMin) {
            swap(numbers, minIndex, index);
        }

        if (index < size - 1) {
            min = numbers[index + 1];
        }

        ++index;
    }
}

/**
 * 选择排序 [0,9,3,2,4,7,8] 从0-N-1选出一个最小值放到0位置上，<br></br>
 * 然后从1-N-1选出一个最小值放到1位置上...
 */
int main_SelectionSort()
{
    int arr[] = {13, 10, 9, 7, 8, 0, 50, 4, 3};
    size_t size = sizeof(arr)/sizeof(int);
    sort(arr, size);

    for (int i = 0; i < size; i++) {
        printf("arr[%d]=%d\n", i, arr[i]);
    }
    
    return 0;
}